package edu.zut.view;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

//将此servlet视为登录页面
public class LoginPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取request中共享的信息
        Object error = request.getAttribute("error");
        System.out.println("error = " + error);
        //将一个登录页面作为响应返回给浏览器
        PrintWriter writer = response.getWriter();
        writer.write("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>用户登录</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<form action=" + request.getContextPath() + "/user/login method=\"get\">\n" +
                "<p>" + (error == null ? "" : error) + "</p>" +
                "    <input type=\"text\" name=\"user_name\" placeholder=\"请输入用户名\"><br>\n" +
                "    <input type=\"password\" name=\"user_psw\" placeholder=\"请输入密码\"><br>\n" +
                "    <input type=\"submit\" value=\"登录\">\n" +
                "</form>\n" +
                "</body>\n" +
                "</html>");
        writer.flush();
        writer.close();
    }
}
